package cn.hnucm.searchesservice.controller;


import cn.hnucm.searchesservice.entity.TbGoodsBasis;
import cn.hnucm.searchesservice.service.TbGoodsBasisService;
import cn.hnucm.searchesservice.utils.MyLog;
import cn.hnucm.searchesservice.utils.Result;
import cn.hnucm.searchesservice.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品明细 前端控制器
 * </p>
 *
 * @author chenjintao
 * @since 2021-12-20
 */
@Api(tags = "商品明细")
@RestController
@CrossOrigin
@RequestMapping("/serchesservice/tbGoodsBasis")
public class TbGoodsBasisController {

    @Autowired
    @Qualifier("tbGoodsBasisServiceImpl")
    private TbGoodsBasisService service;

    /**
     * 获取所有的商品信息
     * @return
     */
    @ApiOperation("获取所有的商品信息")
    @MyLog("获取所有的商品信息")
    @GetMapping("/getAllGoods")
    @ResponseBody
    public Result getAllGoods(){
        List<TbGoodsBasis> goods = service.getAllGoods();
        if (goods == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (goods.isEmpty()) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("goods",goods);
    }

    /**
     * 根据商品号查询商品信息
     * @param goodsId
     * @return
     */
    @ApiOperation("根据商品号查询商品信息")
    @MyLog("根据商品号查询商品信息")
    @GetMapping("/getGoodsById/{goodsId}")
    @ResponseBody
    public Result getGoodsById(@PathVariable("goodsId") Integer goodsId){
        if (goodsId == null){
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        TbGoodsBasis goods = service.getGoodsById(goodsId);
        if (goods == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok("goods",goods);
    }

    /**
     * 根据商品号查询商品信息（作为实体类返回）
     * @param goodsId
     * @return
     */
    @ApiOperation("根据商品号查询商品信息（作为实体类返回）")
    @MyLog("根据商品号查询商品信息（作为实体类返回）")
    @GetMapping("/getGoodsVoById/{goodsId}")
    @ResponseBody
    public TbGoodsBasis getGoodsVoById(@PathVariable("goodsId") Integer goodsId){
        if (goodsId == null){
            return null;
        }
        try {
            TbGoodsBasis goods = service.getGoodsById(goodsId);
            return goods;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }



    /**
     * 根据商品名称和商品简介模糊查询
     * @param describe
     * @return
     */
    @ApiOperation("根据商品名称和商品简介模糊查询")
    @MyLog("根据商品名称和商品简介模糊查询")
    @GetMapping("/getGoodsByDescribe/{describe}")
    @ResponseBody
    public Result  getGoodsByDescribe(@PathVariable("describe") String describe){
        if (describe == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        List<TbGoodsBasis> goods = service.getGoodsByDescribe(describe);
        if (goods == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (goods.isEmpty()) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("goods",goods);
    }

    /**
     * 根据商品分类查询
     * @param goodsType
     * @return
     */
    @ApiOperation("根据商品分类查询")
    @MyLog("根据商品分类查询")
    @GetMapping("/getGoodsByType/{goodsType}")
    @ResponseBody
    public Result  getGoodsByType(@PathVariable("goodsType") String goodsType){
        if (goodsType == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        List<TbGoodsBasis> goods = service.getGoodsByType(goodsType);
        if (goods == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (goods.isEmpty()) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("goods",goods);
    }

    /**
     * 根据卖家查询
     * @param goodsOwerId
     * @return
     */
    @ApiOperation("根据卖家查询")
    @MyLog("根据卖家查询")
    @GetMapping("/getGoodsBySend/{goodsOwerId}")
    @ResponseBody
    public Result  getGoodsBySend(@PathVariable("goodsOwerId") Integer goodsOwerId){
        if (goodsOwerId == null) return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        List<TbGoodsBasis> goods = service.getGoodsBySend(goodsOwerId);
        if (goods == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (goods.isEmpty()) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("goods",goods);
    }

    /**
     * 根据价格区间查询
     * @param highPrice
     * @param lowPrice
     * @return
     */
    @ApiOperation("根据价格区间查询")
    @MyLog("根据价格区间查询")
    @GetMapping("/getGoodsByPrice/{highPrice}/{lowPrice}")
    @ResponseBody
    public Result  getGoodsByPrice(@PathVariable("highPrice") Double highPrice,
                                   @PathVariable("lowPrice") Double lowPrice){
        if (highPrice == null || lowPrice == null)
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        List<TbGoodsBasis> goods = service.getGoodsByPrice(highPrice, lowPrice);
        if (goods == null) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        if (goods.isEmpty()) {
            return Result.error(ResultCode.RESOURCES_NOTFIND);
        }
        return Result.ok("goods", goods);
    }


    /**
     * 添加一条商品信息记录
     * @param tbGoodsBasis
     * @return
     */
    @ApiOperation("添加一条商品信息记录")
    @MyLog("添加一条商品信息记录")
    @PostMapping("/addGoods")
    @ResponseBody
    public Result addGoods(@RequestBody TbGoodsBasis tbGoodsBasis){
        if (tbGoodsBasis == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        Boolean aBoolean = service.addGoods(tbGoodsBasis);
        if (aBoolean == null||!aBoolean) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok();

    }

    /**
     * 通过商品Id删除一条商品记录
     * @param goodsId
     * @return
     */
    @ApiOperation("通过商品Id删除一条商品记录")
    @MyLog("通过商品Id删除一条商品记录")
    @GetMapping("/deleteGoodsById/{goodsId}")
    @ResponseBody
    public Result deleteGoodsById(@PathVariable("goodsId") Integer goodsId){
        if (goodsId == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        Boolean aBoolean = service.deleteGoodsById(goodsId);
        if (aBoolean == null || !aBoolean) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok();
    }



    /**
     * 通过商品Id修改商品信息,其id和修改后的信息一同封装在 TbGoodsBasis中
     * @param tbGoodsBasis
     * @return
     */
    @ApiOperation("通过商品Id修改商品信息,其id和修改后的信息一同封装在 TbGoodsBasis中")
    @MyLog("通过商品Id修改商品信息,其id和修改后的信息一同封装在 TbGoodsBasis中")
    @PostMapping("/updateGoodsById")
    @ResponseBody
    public Result updateGoodsById(@RequestBody TbGoodsBasis tbGoodsBasis){
        if (tbGoodsBasis == null) {
            return Result.error(ResultCode.NOTSATISFACTION_PARAMETER);
        }
        Boolean aBoolean = service.updateGoodsById(tbGoodsBasis);
        if (aBoolean == null || !aBoolean) {
            return Result.error(ResultCode.SERVER_ERROR);
        }
        return Result.ok();
    }

    /**
     * 修改商品的库存
     * @param goodsId
     * @param goodsNum
     * @return
     */
    @ApiOperation("修改商品的库存")
    @MyLog("修改商品的库存")
    @GetMapping("/updateGoodsNumById/{goodsId}/{goodsNum}")
    @ResponseBody
    public Boolean updateGoodsNumById(@PathVariable("goodsId") Integer goodsId,
                                      @PathVariable("goodsNum") Integer goodsNum){
        if (goodsId == null || goodsNum == null || goodsNum < 0){
            return null;
        }
        TbGoodsBasis goodsBasis = new TbGoodsBasis();
        goodsBasis.setGoodsId(goodsId);
        goodsBasis.setGoodsStock(goodsNum);
        try {
        return service.updateGoodsById(goodsBasis);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

}

